//
//  YCShareView.m
//  YouCabService
//
//  Created by gml on 2018/11/29.
//  Copyright © 2018年 NQ. All rights reserved.
//

#import "YCShareView.h"
#import "UIView+Additional.h"
#import "YCCustomAligmentBtn.h"

#define BUTTONWIDTH      80
#define MAXCOUNT      4 // 一行最多4个
@interface YCShareView ()
@property (nonatomic,strong) NSArray *shareBtnTitleArray;
//    所有图片
@property (nonatomic, strong) NSArray  *shareBtnImageArray;

@property (nonatomic, strong) NSArray  *shareTypeArray;

//    整个底部分享面板的 backgroundView
@property (nonatomic, strong) UIView   *bgView;
//    分享面板取消按钮上部的 View
@property (nonatomic, strong) UIView   *topSheetView;
//    取消按钮
@property (nonatomic, strong) UIButton *cancelBtn;
//    所有的分享按钮
@property (nonatomic, copy) NSMutableArray *buttons;

@end

@implementation YCShareView

- (instancetype)initWithShareHeadOprationWith:(NSArray *)titleArray andImageArry:(NSArray *)imageArray shareTypeArr:(NSArray *)typeArr
{
    
    self = [super init];
    if (self) {
        _shareBtnTitleArray = titleArray;
        _shareBtnImageArray = imageArray;
        _shareTypeArray = typeArr;
        self.frame = CGRectMake(0, 0, kMainBoundsWidth, kMainBoundsHeight);
        //    背景，带灰度
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        //    可点击
        self.userInteractionEnabled = YES;
        //    点击背景，收起底部分享面板，移除本视图
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        [self addGestureRecognizer:tapGesture];
        
        //    加载分享面板
        [self loadUIConfig];
    }
    return self;
}

- (void)loadUIConfig
{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.topSheetView];
    [self.bgView addSubview:self.cancelBtn];
    
    //    按钮
    // 目前只实现了两个按钮的情况 其他情况没时间
    if (self.shareBtnTitleArray.count < MAXCOUNT) {
        // 按钮 大小
        CGFloat margin = (kMainBoundsWidth - _shareBtnTitleArray.count * BUTTONWIDTH)/(self.shareBtnTitleArray.count+1);
        for (NSInteger i = 0; i < self.shareBtnTitleArray.count;  i++) {
            CGFloat x = margin*(i+1)+BUTTONWIDTH *i;
            CGFloat y = 0;
            YCCustomAligmentBtn *item = [[YCCustomAligmentBtn alloc] initWithFrame:CGRectMake(x, y, BUTTONWIDTH, BUTTONWIDTH*1.2)];
            item.imageAlignment = BtnImageAlignmentTopType;
            item.tag = [self.shareTypeArray[i] integerValue];
            item.titleLabel.font = [UIFont systemFontOfSize:10 ];
            [item setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [item setTitle:[NSString stringWithFormat:@"%@",self.shareBtnTitleArray[i]] forState:UIControlStateNormal];
            [item setImage:[UIImage imageNamed:self.shareBtnImageArray[i]] forState:UIControlStateNormal];
            [item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.topSheetView addSubview:item];
            [self.buttons addObject:item];
        }
    }else{
        // 按钮大小
        CGFloat width = BUTTONWIDTH;
        CGFloat height = BUTTONWIDTH * 1.2;
        CGFloat margin = (kMainBoundsWidth - BUTTONWIDTH*MAXCOUNT)/(MAXCOUNT+1);
        for (NSInteger i = 0; i < self.shareBtnTitleArray.count;  i++) {
            CGFloat x = i%MAXCOUNT*width+margin;
            CGFloat y = i/MAXCOUNT*height;
            YCCustomAligmentBtn *item = [[YCCustomAligmentBtn alloc] initWithFrame:CGRectMake(x, y, width, height)];
            item.imageAlignment = BtnImageAlignmentTopType;
            item.tag = [self.shareTypeArray[i] integerValue];
            item.titleLabel.font = [UIFont systemFontOfSize:10 ];
            [item setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [item setTitle:[NSString stringWithFormat:@"%@",self.shareBtnTitleArray[i]] forState:UIControlStateNormal];
            [item setImage:[UIImage imageNamed:self.shareBtnImageArray[i]] forState:UIControlStateNormal];
            [item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.topSheetView addSubview:item];
            [self.buttons addObject:item];
        }
        
    }

    //    弹出
    [UIView animateWithDuration:0.25 animations:^{
        self.bgView.frame = CGRectMake(0, kMainBoundsHeight - CGRectGetHeight(self.bgView.frame), kMainBoundsWidth, CGRectGetHeight(self.bgView.frame));
    }];
    
    //    icon 动画
    [self iconAnimation];
    
}
/**
 做一个 icon 依次弹簧动画
 */
- (void)iconAnimation
{
    for (int i = 0; i < self.buttons.count; i++) {
        YCCustomAligmentBtn *btn = self.buttons[i];
        CGFloat centeY = btn.centerY;
        btn.top = centeY;
        // 弹簧动画，参数分别为：时长，延时，弹性（越小弹性越大），初始速度
        [UIView animateWithDuration: 0.7 delay:0.05*i usingSpringWithDamping:0.5 initialSpringVelocity:0.3 options:0 animations:^{
            btn.top = i/MAXCOUNT * (btn.height);
        } completion:nil];
    }

}

/**
 点击取消
 */
- (void)tappedCancel {
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.bgView setFrame:CGRectMake(0, kMainBoundsHeight, kMainBoundsWidth, 0)];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}
-(void) itemClick:(YCCustomAligmentBtn *)btn
{
    [self tappedCancel];
    YCShareViewType type = btn.tag;
    if (self.shareBtnClick) {
        self.shareBtnClick(type);
    }
}
#pragma mark --------------------------- getter

- (UIView *)bgView {
    
    if (_bgView == nil) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor orangeColor];
        //    根据图标个数，计算行数，计算 backgroundView 的高度
        NSInteger index;
        // 每行最多三个 不足三个 平分
        if (_shareBtnTitleArray.count < MAXCOUNT) {
            // 按钮大小
            _bgView.frame = CGRectMake(0, kMainBoundsHeight, kMainBoundsWidth, BUTTONWIDTH*1.6+SAFE_SCREEN_BOTTOM);
        }else{
            if (_shareBtnTitleArray.count % MAXCOUNT == 0) {
                index = _shareBtnTitleArray.count / MAXCOUNT;
            } else {
                index = _shareBtnTitleArray.count / MAXCOUNT + 1;
            }
            _bgView.frame = CGRectMake(0, kMainBoundsHeight, kMainBoundsWidth, (BUTTONWIDTH*1.2) * index+BUTTONWIDTH * 0.4+SAFE_SCREEN_BOTTOM);

        }
        
    }
    return _bgView;
}

- (UIView *)topSheetView {
    
    if (_topSheetView == nil) {
        _topSheetView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_bgView.frame), CGRectGetHeight(_bgView.frame) - CGRectGetHeight(self.cancelBtn.frame))];
        _topSheetView.backgroundColor = UIColorFromRGB(0xF0F0F0);
    }
    return _topSheetView;
}

- (UIButton *)cancelBtn {
    
    if (_cancelBtn == nil) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelBtn.frame = CGRectMake(0, CGRectGetHeight(_bgView.frame) - BUTTONWIDTH*0.4-SAFE_SCREEN_BOTTOM, CGRectGetWidth(_bgView.frame), BUTTONWIDTH*0.4);
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15 ];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.backgroundColor = [UIColor whiteColor];
        [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(tappedCancel) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}


- (NSArray *)buttons {
    
    if (!_buttons) {
        _buttons = [NSMutableArray arrayWithCapacity:5];
    }
    return _buttons;
}

@end
